package com.imachen.ymcc.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.imachen.ymcc.domain.MediaFile;
import com.imachen.ymcc.service.IMediaFileService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
/**
 * @description: 媒体服务消费者
 * @author: AChen
 * @date: 2022/10/11 0011 : 18:06
 * @version: ver1.0
 * @email
 */
@Component
@RocketMQMessageListener(
        consumerGroup = "course-video-push-consumer-group",
        topic = "topic-course-video",
        selectorExpression = "tag-push",
        messageModel = MessageModel.CLUSTERING
)
public class CourseVideoPushConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        // 1.把body数组转换为string字符串
        String messageStr = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        // 2.把字符串转换为对象
        MediaFile mediaFile = JSONObject.parseObject(messageStr, MediaFile.class);
        // 3.调用推流方法
        mediaFileService.handleFile2m3u8(mediaFile);
    }
}
